package com.yizn.service;

import com.spring.BeanPostProcessor;
import com.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * project : my-learn
 * <p>  </p>
 *
 * @author yisl
 * @date 2022-07-22【星期五】
 **/
@Component
public class YiznBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        if (beanName.equals("userService")) {
            Object proxInstance = Proxy.newProxyInstance(YiznBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("执行切面");
                    return method.invoke(bean, args);
                }
            });
            return proxInstance;
        }
        return bean;
    }
}
